package com.linck.im.client;

import cn.hutool.crypto.SecureUtil;
import com.linck.im.client.cache.AccountCache;
import com.linck.im.client.initial.ChatClientInitializer;
import com.linck.im.model.proto.ProtoMsg;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

/**
 * @author: linck
 * @create: 2021-01-25
 */
@Slf4j
public class ImClientApp {

    private String host;

    private int port;

    public ImClientApp(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChatClientInitializer());
            Channel channel = bootstrap.connect(host, port).sync().channel();
            Scanner scanner = new Scanner(System.in);
            while (true) {
                if (!AccountCache.isLogin) {
                    System.out.println("请输入账户名");
                    AccountCache.account = scanner.nextLine().trim();
                    System.out.println("请输入密码");
                    String password = scanner.nextLine().trim();
                    ProtoMsg.LoginInfo loginInfo = ProtoMsg.LoginInfo.newBuilder()
                            .setAccount(ProtoMsg.Account.newBuilder().setAccount(AccountCache.account).setPassword(SecureUtil.sha1(password)).build())
                            .build();
                    long key = AccountCache.count.get();
                    if (!channel.isActive()) {
                        break;
                    }
                    channel.writeAndFlush(loginInfo);
                    log.info("登录中");
                    while (key == AccountCache.count.get()) {
                        Thread.sleep(500);
                    }

                } else {
                    System.out.println("请输入要发送的账户");
                    String account = scanner.nextLine().trim();
                    System.out.println("请输入消息");
                    String content = scanner.nextLine().trim();
                    ProtoMsg.Message message = ProtoMsg.Message.newBuilder()
                            .setFrom(AccountCache.account)
                            .setTo(account)
                            .setContent(content)
                            .setTime(System.currentTimeMillis())
                            .build();
                    channel.writeAndFlush(message);
                }
            }
            channel.closeFuture().sync();
            log.info("连接已关闭，请重新运行");
        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new ImClientApp("127.0.0.1", 8083).run();
    }
}
